package bao;

import org.apache.ibatis.session.SqlSession;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

public class ShopDeal implements IDeal{
    private DataBasePool pool;
    private Connection connection;

    public ShopDeal() {
        this.pool = DataBasePool.getPool();
        try {
            this.connection = pool.getConnection();
            System.out.println(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String find(String id)  {
        try {
            String sql = "SELECT * FROM shop WHERE id=?";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, id);
            ResultSet set = statement.executeQuery();
            if (set.next()) {
                Shop shop = new Shop(set.getString("id"), set.getString("name"),
                        set.getString("room"), set.getString("sales"),
                        set.getString("address"), set.getString("depart"),
                        set.getString("sign"));
                return shop.talk();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public String findAll() throws Exception {
        String sql = "SELECT * FROM shop";
        Statement statement = connection.createStatement();
        ResultSet set = statement.executeQuery(sql);
        List<Shop> list = new ArrayList<>();
        while (set.next()) {
            Shop shop = new Shop(set.getString("id"), set.getString("name"),
                    set.getString("room"), set.getString("sales"),
                    set.getString("address"), set.getString("depart"),
                    set.getString("sign"));
            list.add(shop);
        }
        String result = "";
        for (Shop i : list
        ) {
            result += i.talk();
        }
        return result;
    }

    public int add(Shop s) {
        try {
            String sql = "INSERT INTO shop(id,name,room,sales,address,depart,sign) VALUES(?,?,?,?,?,?,?)";
            connection = pool.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, s.getId());
            statement.setString(2, s.getName());
            statement.setString(3, s.getRoom());
            statement.setString(4, s.getSales());
            statement.setString(5, s.getAdd());
            statement.setString(6, s.getDep());
            statement.setString(7, s.getDate());
            int count=statement.executeUpdate();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int update(Shop s)  {
        try {
            String sql = "UPDATE shop SET name=?,room=?,sales=? WHERE id=?";
            connection = pool.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(4, s.getId());
            statement.setString(1, s.getName());
            statement.setString(2, s.getRoom());
            statement.setString(3, s.getSales());
            int count = statement.executeUpdate();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    public int delete(String s)  {
        try {
            String sql = "DELETE FROM shop WHERE id=?";
            connection = pool.getConnection();
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, s);
            int count = statement.executeUpdate();
            return count;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
